#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <pthread.h>
#include <errno.h>
int gfd;
void *func(void*ptr)
{
	sleep(10);
	close(gfd);
}
int main()
{
	struct sockaddr_in rem;
	rem.sin_family=AF_INET;
	rem.sin_addr.s_addr=inet_addr(argv[1]);
	rem.sin_port=htons(atoi(argv[2]));
	gfd=socket(PF_INET,SOCK_STREAM,0);
	int ret=connect(gfd,(struct sockaddr*)&rem,sizeof(struct sockaddr_in));
	if(ret<0){
		printf("connect to %s:%s failed\n",argv[1],argv[2]);
		exit(-1);
	}
	pthread_t tid;
	pthread_create(&tid,NULL,func,NULL);
	do{
		struct timeval tv={0};
		tv.tv_sec=2;
		fd_set reads;
		FD_ZERO(&reads);
		FD_SET(gfd,&reads);
		ret=select(gfd+1,&reads,NULL,NULL,&tv);
		if(ret==0){
			continue;
		}
		if(FD_ISSET(gfd,&reads)){
			char buf[128];
			recv(gfd,buf,100,0);
			continue;
		}else if(ret>0){
			break;
		}
	}while(ret<0&&(errno==EAGAIN||errno==EINTR));
	return 0;
}
